package com.isczy.eduservice.controller;


import com.isczy.eduservice.service.EduSubjectService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author CZY
 * @since 2020-12-31
 */
@Api(description = "科目 前端控制器")
@RestController
@RequestMapping("/eduservice/edu-subject")
public class EduSubjectController {

    @Autowired
    private EduSubjectService eduSubjectService;

    @ApiOperation(value = "读取excel添加课程分类")
    @PostMapping("addSubject")
    public DataResult addSubject(@RequestBody MultipartFile file){
        try {
            eduSubjectService.addSubject(file,eduSubjectService);
            return DataResult.builder().successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("读取excel添加课程分类失败")
                    .build();
        }
    }

    @ApiOperation(value = "树形结构课程列表")
    @GetMapping("getAllSubject")
    public DataResult getAllSubject(){
        try {
            return DataResult.builder()
                    .data(eduSubjectService.getAllSubject())
                    .successBuild();
        } catch (Exception e) {
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("查询所有课程失败")
                    .build();
        }
    }

}

